package secondriver.abox;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Author : secondriver
 * Created : 2015/11/16
 */
public class TestActivitiesActivity extends Activity implements AdapterView.OnItemClickListener{

    private LinearLayout mRootView;
    private ListView mListView;
    private ArrayAdapter mAdapter;
    private List<Class> mAllActivities;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        mAllActivities = getActivities(this, this.getPackageName(), Arrays.<Class>asList(this.getClass()));
        mAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, android.R.id.text1, mAllActivities);
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(this);

    }

    private void initView() {
        mRootView = new LinearLayout(this);
        mListView = new ListView(this);
        mListView.setId(android.R.id.list);
        mRootView.addView(mListView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        setContentView(mRootView);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Class activity = mAllActivities.get(position);
        //启动指定的Activity
        startActivity(new Intent(this, activity));
    }

    /**
     * 获取所有要测试的Activity Class
     *
     * @param context
     * @param packageName
     * @param excludeActivities
     * @return
     */
    public static List<Class> getActivities(Context context, String packageName, List<Class> excludeActivities) {
        final String TAG = "GET_ACTIVITY";
        List<Class> includeActivities = new ArrayList<>();
        try {
            PackageInfo packageInfo = context.getPackageManager().getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
            if (packageInfo.activities != null) {
                ActivityInfo[] activityInfos = packageInfo.activities;
                Log.d(TAG, "Find " + activityInfos.length + " activity in AndroidManifest.xml.");
                for (ActivityInfo activityInfo : activityInfos) {
                    Class activityClass;
                    String activityName = activityInfo.name;
                    try {
                        activityClass = Class.forName(activityName);
                        if (Activity.class.isAssignableFrom(activityClass)) {
                            includeActivities.add(activityClass);
                        }
                    } catch (ClassNotFoundException e) {
                        Log.d(TAG, "Class not found activity " + activityName + " in package " + packageName);
                    }
                }
            }
            Log.d(TAG, "Found  " + includeActivities.size() + " activity list is :" + Arrays.toString(includeActivities.toArray()));
            if (null != excludeActivities) {
                includeActivities.removeAll(excludeActivities);
            }
            Log.d(TAG, "Last  " + includeActivities.size() + " activity  list is :" + Arrays.toString(includeActivities.toArray()));
        } catch (PackageManager.NameNotFoundException e) {
            Log.d(TAG, "Android system not found package " + packageName);
        }
        return includeActivities;
    }
}
